package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
/*import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;*/
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //@RequiredPermission(name = "员工列表",expression = "employee:list")
    @RequestMapping("/list")
    public String listAll(Model model, @ModelAttribute("qo")EmployeeQueryObject qo){
        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult",pageResult);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        //因为配了 前缀和后缀
        return "employee/list";
    }

    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
            try {
                if (id != null){
                    employeeService.delete(id);
                }
                return new JsonResult(true,"操作成功");
            } catch (Exception e) {
                e.printStackTrace();
                return new JsonResult(false,"操作失败");
            }
    }
    //保存请求
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        if (id != null){
            Employee employee = employeeService.select(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        try {
            if (employee.getId() != null){
                employeeService.update(employee,ids);
            }else {
                employeeService.save(employee,ids);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }
    }
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        try {
            if (ids!=null&&ids.length>0){
                employeeService.batchDelete(ids);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }
    @RequestMapping("checkName")
    @ResponseBody
    public Map checkName(String name){
        HashMap<String, Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByName(name);
        /*if (employee!=null){//已被注册,验证失败
            map.put("valid",false);
        }else {//未被注册,验证成功
            map.put("valid",true);
        }*/
        map.put("valid",employee==null);
        return map;
    }
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载的响应头(让浏览器访问资源的时候以下载的方式打开)
        response.setHeader("Content-Disposition",
                "attachment;filename=employee.xls");
        HSSFWorkbook workbook = employeeService.exportXls();
        //输入到浏览器
        workbook.write(response.getOutputStream());
    }
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        //读取上传的文件的文件输入流,封装到 workbook 中
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        employeeService.importXls(workbook);
        return new JsonResult();
    }
}
